local skill = fk.CreateSkill {
  name = "lvshi__shefu",
}

Fk:loadTranslationTable{
  ["lvshi__shefu"] = "设伏",
  [":lvshi__shefu"] = "其他角色的回合结束时，你可弃置一张牌并对至多X名阵营与其相同的角色各造成1点伤害。（X为中央区内【闪】和【无懈可击】的数量）",
  ["#lvshi__shefu-choose"] = "设伏：你可选择一张牌和至多 %arg 名角色",
  ["$lvshi__shefu1"] = "出奇设伏，变诈之兵并作。",
  ["$lvshi__shefu2"] = "命将设伏，守株待兔。",
}

local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"

skill:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player ~= target
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ids = DIY.getCenterCards(room)
    local cards = table.filter(ids, function(id)
      return Fk:getCardById(id).trueName == "jink" or Fk:getCardById(id).trueName == "nullification"
    end)
    if #cards > 0 then
      local n = #cards
      local targets = table.filter(room.alive_players, function (p) return target == p or ls.isFriend(target, p) end)
      local tos, id, _ = room:askToChooseCardsAndPlayers(player, {
        max_card_num = 1,
        min_card_num = 1,
        max_num = n,
        min_num = 1,
        will_throw = true,
        targets = targets,
        prompt = "#lvshi__shefu-choose:::"..n,
        skill_name = skill.name,
      })
      if #tos > 0 then
        event:setCostData(self, {tos = tos, id = id})
        return true
      end   
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).id, skill.name, player, player)
    for _, p in ipairs(event:getCostData(self).tos) do
      if player.dead then return end
      if not p.dead then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end,
})

skill:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skill